#coding=utf-8
import requests
import re,os


def geturl(name,url):
    data={}
    A = requests.get(url).content.decode('gbk')
    B = re.split(r'<li id="chapter"><a href=|</a></li>',A)
    for html in B:
        if 'html' in html and '\n' not in html:
            C=html.split(r'"')
            data[C[2][1:]]=url+'/'+C[1]
            #存为网页文件
            filepath = os.path.dirname(__file__) + os.sep + '..' + os.sep + 'media' + os.sep + 'book' + os.sep +name
            if not os.path.exists(filepath):
                os.mkdir(filepath)
            path = filepath + os.sep + C[1]
            D = requests.get(url+'/'+C[1]).content.decode('gbk','ignore')

            #上一页
            E1 = str(D).replace('<a type="prev" href="','<a type="prev" href="?name='+name+'&num=')
            #下一页
            E2 = E1.replace('<a type="next" href="','<a type="next" href="?name='+name+'&num=')

            f = open(path,'w',encoding='utf-8')
            f.write(str(E2))
            f.close()
            input('pass')
Data = geturl('庆云仙','https://www.kuwx.net/ku/314/314311/')
